extends KinematicBody2D

class_name PlatformerController2D

# Set these to the name of your action (in the Input Map)
export var input_left : String = "ui_left"
export var input_right : String = "ui_right"
export var input_jump : String = "ui_up"
export (float, 0, 100) var move_speed : float = 5
export var move_speed_max : int = 500
export (float, 0, 100) var friction : float = 9
export var jump_height : int = 1200
export (float, 0, 100) var gravity : float = 1
export var gravity_max : int = 2500


var velocity = Vector2(0,0)


func _physics_process(delta):
	# 左右移动
	var v = Input.get_action_strength(input_right) - Input.get_action_strength(input_left)
	if v != 0:
		velocity.x = lerp(
			velocity.x, 
			v * move_speed_max, 
			move_speed * delta
		)
	else:
		velocity.x = lerp(
			velocity.x, 
			0, 
			friction * delta
		)
	
	# 跳跃
	if Input.is_action_pressed(input_jump):
		if is_on_floor():
			velocity.y = -jump_height
	
	move_and_slide(velocity, Vector2.UP)
	
	# 根据碰撞信息设置 y 轴的值
	if is_on_ceiling():
		velocity.y = 0
		velocity.y = lerp(velocity.y, gravity_max, gravity * delta)
	elif not is_on_floor():
		velocity.y = lerp(velocity.y, gravity_max, gravity * delta)
	else:
		velocity.y = 0



